<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>原型</title>
</head>
<body>

    <script type="text/javascript">
        //prototype  原型  相当于css的class
        
        var arr1 = new Array(12,3,242,90);
        var arr2 = new Array(1,3,5);

        arr1.sum = function(){         //arr1.sum相当于行间样式
            var result = 0;
            for(var i=0;i<this.length;i++){
                result += this[i];
            }
            return result;
        }

        alert(arr1.sum());        //347
        //alert(arr2.sum());        //此时这条会报错  因为arr1.sum = function()没有作用到arr2

     
      //当把方法写成以下情况时，相当于同时作用给arr1,arr2
        Array.prototype.sum = function(){
      	   var result = 0;
           for(var i=0;i<this.length;i++){
           	   result += this[i];
           }
           return result;
        }

        alert(arr1.sum());      //347
        alert(arr2.sum());      //9

    </script>
	
</body>
</html>